package com.noah.note.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;

import com.noah.note.R;
import com.noah.note.global.BaseActivity;
import com.noah.note.global.Const;
import com.noah.note.presenter.LoginPresenter;
import com.noah.note.utils.SPUtils;
import com.noah.note.view.LoginView;

public class SplashActivity extends BaseActivity<LoginPresenter> implements LoginView{
    @Override
    protected LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {//解决按 home 键之后再次点击桌面图标进来从新进入的 bug
            finish();
            return;
        }
        checkToken();
    }

    private void checkToken() {
        String token = (String) SPUtils.get(Const.SP_TOKEN, "");
        if(!TextUtils.isEmpty(token)){
            mPresenter.login(token);
        } else {
            goActivity(LoginActivity.class);
        }
    }

    @Override
    public void loginSuccess() {
        goActivity(HomeActivity.class);
    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void loginFail() {
        goActivity(LoginActivity.class);
    }
}
